package com.gao.unsafe;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;


public class SetTest {
    public static void main(String[] args) {

//        同理可证，HashSet也是不安全的
//        解决方案：
//        1、Set<String> set = Collections.synchronizedSet(new HashSet<>());
//        2、CopyOnWriteArraySet


        Set<String> set = new CopyOnWriteArraySet();
        for (int i = 1; i <= 30; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }
}
